package com.lzy.netty.utils;

import lombok.Getter;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;

public class CuratorConfig {

    private static final String ZK_ADDRESS = "127.0.0.1:2181";  // 单机/集群 ip:port
    private static final Integer ZK_CONNECTION_TIMEOUTMS = 30 * 1000; // 连接超时的时间
    private static final Integer ZK_SESSION_TIMEOUTMS = 3 * 1000;     // 会话超时的时间
    private static final Integer ZK_SLEEP_TIME_RETRY = 2 * 1000;          // 每次重试间隔的时间
    private static final Integer ZK_MAX_RETRIES = 3;                // 最大重试次数
    private static final String ZK_NAMESPACE = "lzy-wechat";        // 命名空间(root根节点的名称)

    // Curator(Zookeeper)客户端
    @Getter
    private static CuratorFramework client;  //客户端

    static {
        // 定义重试策略
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(ZK_SLEEP_TIME_RETRY, ZK_MAX_RETRIES);

        // 声明初始化客户端
        client = CuratorFrameworkFactory
                .builder()
                .connectString(ZK_ADDRESS)
                .sessionTimeoutMs(ZK_SESSION_TIMEOUTMS)
                .connectionTimeoutMs(ZK_CONNECTION_TIMEOUTMS)
                .retryPolicy(retryPolicy)
                .namespace(ZK_NAMESPACE)
                .build();
        client.start(); //启动客户端
    }
}

